import os
import json

from .Nvm_Task import Nvm_Task
from .Nvm_Book import Nvm_Excel
from .cfg.Nvm_Global import Nvm_Gloable

class NvmGeneOpera(object):
    def __init__(self):
       self.nvm_task = Nvm_Task()
       self.nvm_excel = Nvm_Excel()
       self.diddata  = []
    
    def NvmModelGeneFloder(self, excel_path):
        # self.nvm_task.NvmTaskMkDir('Nvm')
        self.nvm_task.NvmTaskSetExcelName(excel_path)
        self.nvm_task.NvmTaskMklog()

    def NvmModelOpExcel(self, excel_path):
        self.nvm_excel.NvmExcelRead(excel_path, Nvm_Gloable.def_sheetindex)
        self.nvm_task.NvmTaskGetDid(self.nvm_excel, Nvm_Gloable.def_sheetstartline, Nvm_Gloable.def_sheetendline)
    
    def NvmModelGeneCode(self):
        self.nvm_task.NvmTaskToFile()
   
    def NvmModelGetExcelRows(self):
        return self.nvm_excel.NvmExcelGetRows()
    
    def NvmModelGetExcelCols(self):
        return self.nvm_excel.NvmExcelGetCols()
    
    def NvmModelGetExcelData(self, row, col):
        return self.nvm_excel.NvmExcelGetData(row, col)

    def _NvmModelGetDidData(self):
        return self.nvm_task.NvmTaskGetDidData()
    
    def NvmModelGeneRules(self, module_name, rules_name):
        rules_file = os.path.join("project", "configure", module_name, "rules", rules_name + '.json')
        test_ruels1 = {}
        test_ruels2 = {}
        self.diddata = self._NvmModelGetDidData()
        self.didlist = []
        self.byte_index = 0
        with open(rules_file, 'w', encoding = 'utf-8') as f:
            # f.truncate(0)    # clear rules_name + '.json'
            for i in range(len(self.diddata)):
                diddict = {}
                diddict["did_id"] = self.diddata[i].did
                diddict["did_name"] = self.diddata[i].name
                diddict["did_name_c"] = self.diddata[i].name_c
                diddict["did_len"] = self.diddata[i].bytesize
                self.signallist = []
                for j in range(len(self.diddata[i].msigs)):
                    sigdict = {}
                    self.byte_flag = False
                    if (self.diddata[i].msigs[j].name == "_"):
                        sigdict["byte_name"] = self.diddata[i].name
                    else:
                        sigdict["byte_name"] = self.diddata[i].msigs[j].name
                    sigdict["byte_name_c"] = self.diddata[i].msigs[j].name_c
                    sigdict["byte_start"] = self.diddata[i].msigs[j].byte_start
                    sigdict["byte_size"] = self.diddata[i].msigs[j].byte_size
                    sigdict["bit_start"] = self.diddata[i].msigs[j].bit_start
                    sigdict["bit_size"] = self.diddata[i].msigs[j].bit_size
                    sigdict["info"] = self.diddata[i].msigs[j].info
                    sigdict["minvalue"] = self.diddata[i].msigs[j].minvalue
                    sigdict["maxvalue"] = self.diddata[i].msigs[j].maxvalue
                    sigdict["defaultvalue"] = self.diddata[i].msigs[j].defaultvalue
                    if self.byte_index != self.diddata[i].msigs[j].byte_start:
                        self.byte_index = self.diddata[i].msigs[j].byte_start
                        self.byte_flag = True
                    sigdict["byte_flag"] = self.byte_flag 
                    self.signallist.append(sigdict)
                diddict["Signals"] = self.signallist
                self.didlist.append(diddict)
            test_ruels2["functions"] = self.didlist
            test_ruels2["str_desc"] = "description"
            test_ruels1["rules"] = test_ruels2
            # test_ruels1.update({'rules': test_ruels2})
            f.write(json.dumps(test_ruels1, indent = 4, ensure_ascii = False))
            f.close()
    
    def NvmModelGetGolbalCfgDict(self):
        self.global_cfg_dict = {}
        self.global_cfg_dict['def_didnum'] = Nvm_Gloable.def_didnum
        self.global_cfg_dict['def_didname'] = Nvm_Gloable.def_didname
        self.global_cfg_dict['def_didnamec'] = Nvm_Gloable.def_didnamec
        self.global_cfg_dict['def_didsize'] = Nvm_Gloable.def_didsize
        self.global_cfg_dict['def_didbyte'] = Nvm_Gloable.def_didbyte
        self.global_cfg_dict['def_didbit'] = Nvm_Gloable.def_didbit
        self.global_cfg_dict['def_didsigname'] = Nvm_Gloable.def_didsigname
        self.global_cfg_dict['def_didsignamec'] = Nvm_Gloable.def_didsignamec
        self.global_cfg_dict['def_didsiginfo'] = Nvm_Gloable.def_didsiginfo
        self.global_cfg_dict['def_didsigmin'] = Nvm_Gloable.def_didsigmin
        self.global_cfg_dict['def_didsigmax'] = Nvm_Gloable.def_didsigmax
        self.global_cfg_dict['def_didsigdefault'] = Nvm_Gloable.def_didsigdefault
        self.global_cfg_dict['def_didsigtestvalue'] = Nvm_Gloable.def_didsigtestvalue
        self.global_cfg_dict['def_sheetindex'] = Nvm_Gloable.def_sheetindex
        self.global_cfg_dict['def_sheetstartline'] = Nvm_Gloable.def_sheetstartline
        self.global_cfg_dict['def_sheetendline'] = Nvm_Gloable.def_sheetendline
        self.global_cfg_dict['str_can_use'] = Nvm_Gloable.str_can_use
        self.global_cfg_dict['def_testonoff'] = Nvm_Gloable.def_testonoff
        self.global_cfg_dict['def_defaultonoff'] = Nvm_Gloable.def_defaultonoff
        return self.global_cfg_dict